package com.szy.news.control;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.System;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;
import android.widget.Toast;

/** 
 * @author 辛晓博
 * @version 创建时间：2014年11月29日 上午9:38:59 
 * 这是屏幕背光调节的实现 
 */

public class LightnessControl {
	
	/**android2.1以后的版本提供了背光亮度自动调节功能，
	 * 因此需要先判断是否开启自动调节
	 */
	public static boolean isAutoBrightness(Activity act){
		boolean automicAutoBrightness = false;
		ContentResolver acontentResolver = act.getContentResolver();
		try {
			automicAutoBrightness = Settings.System.getInt(acontentResolver,
					Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			Toast.makeText(act, "无法获取系统背光亮度", Toast.LENGTH_SHORT).show();
		}
		return automicAutoBrightness;
	}
	
	
	//改变亮度
	public static void SetLightness(Activity act, int value){
		try {
			System.putInt(act.getContentResolver(),System.SCREEN_BRIGHTNESS,value);
			WindowManager.LayoutParams lp = act.getWindow().getAttributes();
			lp.screenBrightness = (value<=0?1:value)/255f;
			act.getWindow().setAttributes(lp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(act, "无法调节背光亮度", Toast.LENGTH_SHORT).show();
		}
	}
	
	//获取系统背光亮度值
	public static int getLightness(Activity act){
		return System.getInt(act.getContentResolver(), System.SCREEN_BRIGHTNESS, -1);
	}
	
	//停止系统背光亮度的自动调节
	public static void stopAutoBrightness(Activity act){
		Settings.System.putInt(act.getContentResolver(), 
				Settings.System.SCREEN_BRIGHTNESS_MODE, 
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	}
	
	//开启系统背光亮度自动调节
	private void startAutoBrightness(Context con) {
		Settings.System.putInt(con.getContentResolver(), 
				Settings.System.SCREEN_BRIGHTNESS_MODE, 
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

	}

}
